package com.akaovi.exp.utils;

import java.io.*;
import java.util.Random;
import java.util.Scanner;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/10/11 18:07
 * @since 2022/10/11 18:07
 */
public class Utils {
    private static final Scanner scan = new Scanner(System.in);

    public static int getOneInt() {
        return scan.nextInt();
    }

    public static String getThreeInt() {
        return scan.next();
    }

    public static boolean isPrime(int n) {
        if (n <= 3) {
            return n > 1;
        }
        if (n % 6 != 1 && n % 6 != 5) {
            return false;
        }
        int sqrt = (int) Math.sqrt(n);
        for (int i = 5; i <= sqrt; i += 6) {
            if (n % i == 0 || n % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 冒泡排序
     *
     * @param arr
     */
    public static void bubbleSort(int[] arr) {
        int temp;
        for (int i = 0; i < arr.length - 1; ++i) {
            for (int j = i + 1; j < arr.length; ++j) {
                if (arr[j] < arr[i]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    /**
     * 选择排序
     */
    public static void selectSort(int[] arr) {
        int temp, minIndex;
        for (int i = 0; i < arr.length - 1; ++i) {
            minIndex = i;
            for (int j = i; j < arr.length; ++j) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    /**
     * 插入排序
     */
    public static void insertSort(int[] arr) {
        int temp, j;
        for (int i = 1; i < arr.length; ++i) {
            temp = arr[i];
            j = i;
            while (j > 0 && temp < arr[j - 1]) {
                arr[j] = arr[j - 1];
                --j;
            }
            if (j != i) {
                arr[j] = temp;
            }
        }
    }

    public static int[] createIntArr(int length) {
        int[] arr = new int[length];
        Random random = new Random();
        for (int i = 0; i < length; ++i) {
            arr[i] = random.nextInt(100);
        }
        return arr;
    }

    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T clone(T obj) {
        T cloneObj = null;
        //写入字节流
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream obs = new ObjectOutputStream(out);
            obs.writeObject(obj);
            obs.close();

            //分配内存，写入原始对象，生成新对象
            ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(ios);
            //返回生成的新对象
            cloneObj = (T) ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cloneObj;
    }


}
